package com.share.one;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.Logger;
import org.junit.Test;

public class Sender_1 {
	private static Logger log = Logger.getLogger("runLog");
	
	@Test
	public void test() throws Exception{
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
				"sunxj", 
				"123123", 
				"tcp://localhost:61616");
		Connection connection = connectionFactory.createConnection();
		connection.start();
		
		Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
		Destination destination = session.createQueue("test1_queue");
		//MessageProducer messageProducer = session.createProducer(destination);
		MessageProducer messageProducer = session.createProducer(null);
		//messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
		
		for (int i = 0; i < 5; i++) {
			TextMessage textMessage = session.createTextMessage();
			textMessage.setText("我是message" + i);
			//持久化与timeToLive：非持久化方式timeToLive才有效
			messageProducer.send(destination, textMessage, DeliveryMode.PERSISTENT, i, 1000*10);
			//System.out.println("生产者： " + textMessage.getText());
			//logger.info("生产者： " + textMessage.getText());
		}
		
		//session.commit();//session采用事务
		
		if (connection != null) {
			connection.close();
		}
		
		log.info("info");
		log.debug("debug");
		log.error("error");
		log.warn("warn");
		
	}

}
